
********************************************************************************
********************************************************************************
********************************** FIGURE 2 ************************************
************* VARIATION IN VISIT AMOUNTS, DENIALS AND RESUBMISSIONS ************
********************************************************************************
********************************************************************************
set scheme uncluttered

//use the data that were created in the summary stats do file on the BEA server
use `"$output/summary_stats/variation/bined_variation.dta"', clear 

gen lev_line_item_value = line_item_value
gen ln_value = ln(line_item_value)
replace line_item_value = ln_value

//bin fee into 100 equally sized bins, keeping median foreach bin
xtile bin_fee_tmp = line_item_value, n(100)
bys bin_fee_tmp: gegen bin_fee = median(line_item_value)


//collapse by fee within payer
gcollapse (mean) R_1 D_0 (count) obs, by(pay_type_pooled bin_fee) 

forval x = 1/3 {
	
	if `x' == 1 {
		local paystring "Medicare"
	}
	
	else if `x' == 2 {
		local paystring  "Medicaid"
	}
	
	else if `x' == 3 {
		local paystring  "Commercial"
	}
	
	di "`paystring'"

	cap graph drop resubmission_`paystring'
	cap graph drop denial_`paystring'

	#delimit ;
	gr tw histogram bin_fee [fw=obs] if pay_type_pooled == `x' & bin_fee < 5000, 
		bin(50) frac yaxis(1) color(orange) fcolor(orange) color(%50) || 
		scatter R_1 bin_fee if pay_type_pooled == `x' & bin_fee < 5000, 
		m(Oh) msize(medsmall) mlw(medium) mcolor(black) yaxis(2) 
		xti("Log Line Item Value (binned)", size(large)) 
		yti("Fraction of Line Items", axis(1) size(large)) 
		yti("Mean Resubmission Probability", axis(2) size(large))  
		xscale(r(0 10)) xlabel(0(2)10) yscale(r(0 0.052) axis(1)) 
		ylabel(0(0.01)0.05, axis(1)) yscale(r(0 1) axis(2)) 
		ylabel(0(0.2)1, axis(2)) name(resubmission_`paystring') leg(off);
	#delimit cr
	
	gr export `"$output/paper_inputs/variation_fee_resub_`paystring'.pdf"', replace 
	gr export `"$output/paper_inputs/variation_fee_resub_`paystring'.png"', replace 

	#delimit ;
	gr tw histogram bin_fee [fw=obs] if pay_type_pooled == `x' & bin_fee < 5000, 
		bin(50) frac yaxis(1) color(orange) fcolor(orange) color(%50) || 
		scatter D_0 bin_fee if pay_type_pooled == `x' & bin_fee < 5000, 
		m(Oh) msize(medsmall) mlw(medium) mcolor(black) yaxis(2) 
		xti("Log Line Item Value (binned)", size(large)) 
		yti("Fraction of Line Items", axis(1) size(large)) 
		yti("Mean Denial Probability", axis(2) size(large)) 
		xscale(r(0 10)) xlabel(0(2)10) yscale(r(0 0.052) axis(1)) 
		ylabel(0(0.01)0.05, axis(1)) yscale(r(0 1) axis(2)) 
		ylabel(0(0.2)1, axis(2)) name(denial_`paystring') leg(off);
	#delimit cr 

	gr export `"$output/paper_inputs/variation_fee_denial_`paystring'.pdf"', replace 
	gr export `"$output/paper_inputs/variation_fee_denial_`paystring'.png"', replace 

}



